﻿using HCApp.UI.Windows.Views;
using System.Windows.Controls;

using System.Linq;
using System.Collections.Generic;
using MainAppEntryPrismWpf.ViewModels;
using System;
using System.Windows;

namespace MainAppEntryPrismWpf.Views
{
    /// <summary>
    /// Interaction logic for ChuKuManagePage
    /// </summary>
    public partial class ChuKuManagePage : ContentPageBase
    {

        public DataClassesSCDBDataContext SCDBDataContent;

        private int MaxBH = -1;

        private DSZX_CPCRKB SelectedYG = new DSZX_CPCRKB();

        private List<DSZX_CPCRKB> ygxxList;    //所有列表  同数据库中数据同
        private List<DSZX_CPCRKB> ygSignedList;

        private int sumPS = 0;


       // psTB

        public ChuKuManagePage()
        {
            InitializeComponent();


            if (DataContext is ChuKuManagePageViewModel viewModel)
            {
                //  viewModel.hybridWebView = hybridWebView;

                viewModel.DialogService.AlertAsync("Something");
            }


            ygSignedList = new List<DSZX_CPCRKB>();
            ygxxList = new List<DSZX_CPCRKB>();

            SCDBDataContent = new DataClassesSCDBDataContext();

            datagrid.ItemsSource = (from data in SCDBDataContent.DSZX_CPCRKB
                                                 where data.CKBZ==0
                                    where data.CJH == "单丝"
                                    select data).ToList();

            ggYG.ItemsSource = (from data in SCDBDataContent.DSZX_YGXXB select data.YGXM).ToList();

            ggSelect.ItemsSource = (from data in SCDBDataContent.DSZX_CPGGB where data.CJH == "单丝" select data.CPGG).ToList();
        }

        private void delete_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            
        }
        private void add_Click(object sender, System.Windows.RoutedEventArgs e)
        {

            string selectedResult = (this.manualSelect.SelectedItem as ComboBoxItem).Content.ToString();


            if (MessageBox.Show("请您核对这些待出库的数据.\n\n确认出库?",
                     "确认出库 ？",
                     MessageBoxButton.YesNo,
                     MessageBoxImage.Question) == MessageBoxResult.No)
            //    if (!WindowDialog.ShowDialog("请您核对这些待出库的数据.\n\n确认出库?").IsYes)
            {
                // Do something here
                return;
            }
            else
            {
                if (selectedResult == "列表选择")
                {

                    sumPS = 0;
                    if (ygSignedList.Count > 0)
                    {
                        //   foreach (var item in ygSignedList)
                        for (int i = 0; i < ygSignedList.Count; i++)

                        {
                            var toModifyObject = SCDBDataContent.DSZX_CPCRKB.Where(a => a.ID == ygSignedList[i].ID).FirstOrDefault();

                            if (toModifyObject != null)
                            {
                                //   toModifyObject.CZR = this.czrTB.Text.ToString();
                                toModifyObject.CZR = (this.ggYG.SelectedItem as string);
                                toModifyObject.CKSJ = DateTime.Now;
                                toModifyObject.CKBZ = 1;  //已经出库

                                sumPS = sumPS + (int)toModifyObject.PS;
                                SCDBDataContent.SubmitChanges();
                            }
                            else
                            {
                                MessageBox.Show("没有找到！！！");
                                return;
                            }


                        }
                    }


                }
                else if (selectedResult == "手动输入")
                {
                    MessageBox.Show("请选择需要的规格和盘数.\n\n");

                    int neededCounts = Convert.ToInt32(this.psTB.Text);
                    string neededGG = ggSelect.SelectedItem as string;

                    var gotList = (from data in SCDBDataContent.DSZX_CPCRKB
                                   where data.CKBZ == 0
                                   where data.CJH == "单丝" && data.CPGG == neededGG
                                   orderby data.RKSJ

                                   select data).ToList();

                    if (gotList != null && gotList.Count > 0)
                    {

                        if (gotList.Count >= neededCounts)
                        {
                            for (int i = 0; i < neededCounts; i++)
                            {
                                var toModifyObject = SCDBDataContent.DSZX_CPCRKB.Where(a => a.ID == gotList[i].ID).FirstOrDefault();

                                if (toModifyObject != null)
                                {
                                    //   toModifyObject.CZR = this.czrTB.Text.ToString();
                                    toModifyObject.CZR = (this.ggYG.SelectedItem as string);
                                    toModifyObject.CKSJ = DateTime.Now;
                                    toModifyObject.CKBZ = 1;  //已经出库

                                    sumPS = sumPS + (int)toModifyObject.PS;
                                    SCDBDataContent.SubmitChanges();
                                }
                                else
                                {
                                    MessageBox.Show("没有找到！！！");
                                    return;
                                }
                            }

                        }
                        else
                        {
                            MessageBox.Show("没有找到足够的该规格的盘数");
                            return;
                        }
                    }
                    else
                    {
                        MessageBox.Show("没有找到");
                        return;
                    }

                }
                else
                {

                }

                this.psTB.Text = sumPS.ToString();
                ygxxList = (from data in SCDBDataContent.DSZX_CPCRKB
                            where data.CKBZ == 0
                            where data.CJH == "单丝"
                            select data).ToList();
                datagrid.ItemsSource = ygxxList;

                MessageBox.Show("恭喜你，你所选择的货物已经出库。\n\n已成功出库","出库提示");
           

            }
          

        }

        private void datagrid_SelectionChanged(object sender, Syncfusion.UI.Xaml.Grid.GridSelectionChangedEventArgs e)
        {

            ygSignedList.Clear();
            for (int i = 0; i < this.datagrid.SelectedItems.Count; i++)
            {
                ygSignedList.Add(this.datagrid.SelectedItems[i] as DSZX_CPCRKB);
            }
          
          this.zlTB.Text=  ygSignedList.Sum(a => a.CPZL).ToString();
            this.psTB.Text = ygSignedList.Sum(a => a.PS).ToString();
        }

        private void manualSelect_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string selectedResult = (this.manualSelect.SelectedItem as ComboBoxItem).Content.ToString();

            if (selectedResult=="列表选择")
            {
                if (this.psTB != null)
                {
                    this.psTB.IsEnabled = false;
                }


            }
            else if (selectedResult == "手动输入")
            {
               
                if (this.psTB != null)
                {
                    this.psTB.IsEnabled = true;
                }
            }
            else
            {

            }

           //(this.xbSelect.SelectedItem as ComboBoxItem).Content.ToString()
           //   MessageBox.Show((this.manualSelect.SelectedItem as ComboBoxItem).Content.ToString()); 
        }

        private void ContentPageBase_Loaded(object sender, RoutedEventArgs e)
        {
            SCDBDataContent = new DataClassesSCDBDataContext();

            datagrid.ItemsSource = (from data in SCDBDataContent.DSZX_CPCRKB
                                    where data.CKBZ == 0
                                    where data.CJH == "单丝"
                                    select data).ToList();

            ggYG.ItemsSource = (from data in SCDBDataContent.DSZX_YGXXB select data.YGXM).ToList();

            ggSelect.ItemsSource = (from data in SCDBDataContent.DSZX_CPGGB where data.CJH == "单丝" select data.CPGG).ToList();
        }
    }
}
